package com.sip.stream.rxbus;

import io.reactivex.disposables.Disposable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class CacheUtils {
    private final Map<Object, List<Disposable>> disposablesMap;
    private final Map<Class, List<TagMessage>> stickyEventsMap;

    private CacheUtils() {
        this.stickyEventsMap = new ConcurrentHashMap();
        this.disposablesMap = new ConcurrentHashMap();
    }

    public static CacheUtils getInstance() {
        return Holder.CACHE_UTILS;
    }

    public void addStickyEvent(Object obj, String str) {
        Class classFromObject = Utils.getClassFromObject(obj);
        synchronized (this.stickyEventsMap) {
            List<TagMessage> list = this.stickyEventsMap.get(classFromObject);
            if (list == null) {
                ArrayList arrayList = new ArrayList();
                arrayList.add(new TagMessage(obj, str));
                this.stickyEventsMap.put(classFromObject, arrayList);
            } else {
                for (int size = list.size() - 1; size >= 0; size--) {
                    if (list.get(size).isSameType(classFromObject, str)) {
                        Utils.logW("The sticky event already added.");
                        return;
                    }
                }
                list.add(new TagMessage(obj, str));
            }
        }
    }

    public void removeStickyEvent(Object obj, String str) {
        Class classFromObject = Utils.getClassFromObject(obj);
        synchronized (this.stickyEventsMap) {
            List<TagMessage> list = this.stickyEventsMap.get(classFromObject);
            if (list == null) {
                return;
            }
            int size = list.size() - 1;
            while (true) {
                if (size < 0) {
                    break;
                } else if (list.get(size).isSameType(classFromObject, str)) {
                    list.remove(size);
                    break;
                } else {
                    size--;
                }
            }
            if (list.size() == 0) {
                this.stickyEventsMap.remove(classFromObject);
            }
        }
    }

    public TagMessage findStickyEvent(Class cls, String str) {
        synchronized (this.stickyEventsMap) {
            List<TagMessage> list = this.stickyEventsMap.get(cls);
            TagMessage tagMessage = null;
            if (list == null) {
                return null;
            }
            int size = list.size() - 1;
            while (true) {
                if (size < 0) {
                    break;
                } else if (list.get(size).isSameType(cls, str)) {
                    tagMessage = list.get(size);
                    break;
                } else {
                    size--;
                }
            }
            return tagMessage;
        }
    }

    public void addDisposable(Object obj, Disposable disposable) {
        synchronized (this.disposablesMap) {
            List<Disposable> list = this.disposablesMap.get(obj);
            if (list == null) {
                ArrayList arrayList = new ArrayList();
                arrayList.add(disposable);
                this.disposablesMap.put(obj, arrayList);
            } else {
                list.add(disposable);
            }
        }
    }

    public void removeDisposables(Object obj) {
        synchronized (this.disposablesMap) {
            List<Disposable> list = this.disposablesMap.get(obj);
            if (list == null) {
                return;
            }
            for (Disposable disposable : list) {
                if (disposable != null && !disposable.isDisposed()) {
                    disposable.dispose();
                }
            }
            list.clear();
            this.disposablesMap.remove(obj);
        }
    }

    public static class Holder {
        private static final CacheUtils CACHE_UTILS = new CacheUtils();

        private Holder() {
        }
    }
}
